﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Todo.Infrastructure.DatabaseContext.EntityConfigurations
{
    public class TodoGroupConfig : IEntityTypeConfiguration<TodoGroup>
    {
        public void Configure(EntityTypeBuilder<TodoGroup> builder)
        {
            builder.ToTable("todo_group");

            builder.HasKey(_ => _.Id);
            builder.Property(_ => _.Id)
                .HasColumnName("id")
                .HasMaxLength(36)
                .IsRequired();

            builder.Property(_ => _.Name)
                .HasColumnName("name");

            builder.OwnsOne(_ => _.CreationInfo, info =>
            {
                info.Property(_ => _.UserId)
                .HasColumnName("user_id");
                info.Property(_ => _.CreationTime)
                .HasColumnName("creation_time");
            });

            builder.Property(_ => _.TodoType).HasColumnName("todo_type");

            builder.HasMany(_ => _.Todos).WithOne().HasForeignKey("todo_group_id").OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(_ => _.Settings).WithOne().HasForeignKey<TodoGroupSettings>("todo_group_id").IsRequired().OnDelete(DeleteBehavior.Cascade);
        }
    }
}
